package com.hualan.dao.impl;

import com.hualan.bean.PanghuShop;
import com.hualan.dao.ShopDao;
import com.hualan.utils.C3p0Util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ShopDaoImpl implements ShopDao {
    //获取shop列表
    @Override
    public List<PanghuShop> selectPage(String keyWord, String phone, String startTime, String endTime, Integer pageNum, Integer pageSize) {
        String sql = "select * from panghu_shop where 1=1";
        List<Object> params = new ArrayList<>();
        //动态sql
        if (keyWord != null && !keyWord.isEmpty()) {
            sql = sql + " and shop_name like "+"'%" + keyWord + "%'";
//            params.add("'%" + keyWord + "%'");
        }
        if (phone != null && !phone.isEmpty()) {
            sql = sql + " and shop_phone like  " + "'%" + phone + "%'";
//            params.add("'%" + phone + "%'");
        }
        if (startTime != null && !startTime.isEmpty() && endTime != null && !endTime.isEmpty()) {
            //将日期格式化
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                startTime = dateFormat.format(dateFormat.parse(startTime));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            try {
                endTime = dateFormat.format(dateFormat.parse(endTime));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            sql = sql + " And add_time Between  ? and ?";
            params.add(startTime);
            params.add(endTime);
        }
        pageNum = (pageNum - 1) * pageSize;
        sql += " limit " + pageNum + "," + pageSize;
        System.out.println(sql);
        List<PanghuShop> shopList = C3p0Util.query(sql, PanghuShop.class, params.toArray());
        //TODO:到时候遍历shop设置小区名字
        if (!shopList.isEmpty()) {
            for (PanghuShop panghuShop : shopList) {
                String name = selectCommunityNameByShopId(panghuShop.getShopId());
                panghuShop.setCommunityName(name);
            }
            return shopList;
        }
        return null;
    }

    //获取shop个数
    @Override
    public Long selectShopCount() {
        Map<String, Object> map = C3p0Util.queryOne("select count(shop_id) as count from panghu_shop");
        if (map != null && !map.isEmpty()) {
            return (Long) map.get("count");
        } else {
            //否则反空值
            return null;
        }
    }

    //根据shopId查询社区名字
    @Override
    public String selectCommunityNameByShopId(Integer shopId) {
        String sql = "SELECT c.community_name AS communityName FROM panghu_community AS c,panghu_shop_community AS sc WHERE c.community_id=sc.panghu_community_id AND panghu_shop_id=?";
        Map<String, Object> map = C3p0Util.queryOne(sql, shopId);
        if (map != null && !map.isEmpty()) {
            return (String) map.get("communityName");
        } else {
            return null;
        }
    }

    //新增shop
    @Override
    public int addShop(String shopAddress, String shopDescribe, String shopImage, String shopLatitude, String shopLongitude, String shopName, String shopPhone) {
        String sql = "INSERT INTO panghu_shop(shop_address,shop_describe,shop_image,shop_latitude,shop_longitude,shop_name,shop_phone) VALUES(?,?,?,?,?,?,?)";
        if (shopLatitude == null) {
            shopLatitude = "";
        }
        if (shopLongitude == null) {
            shopLongitude = "";
        }
        int row = C3p0Util.update(sql, shopAddress, shopDescribe, shopImage, shopLatitude, shopLongitude, shopName, shopPhone);
        return Math.max(row, 0);
    }

    //修改shop
    @Override
    public int update(String addTime, String shopAddress, String shopCity, String shopDescribe, String shopDistrict, String shopImage, String shopLatitude, String shopLongitude, String shopName, String shopPhone, String shopProvince, String shopId) {
        String sql = "UPDATE panghu_shop SET add_time=? ,shop_address=?,shop_city=?,shop_describe=?,shop_district=?,shop_image=?,shop_latitude=?,shop_longitude=?,shop_name=?,shop_phone=?,shop_province=? WHERE shop_id=?";
        if (shopCity == null || shopDistrict == null || shopProvince == null) {
            shopCity = "";
            shopDistrict = "";
            shopProvince = "";
        }
        if (shopLatitude == null || shopLongitude == null) {
            shopLatitude = "";
            shopLongitude = "";
        }
        int row = C3p0Util.update(sql, addTime, shopAddress, shopCity, shopDescribe, shopDistrict, shopImage, shopLatitude, shopLongitude, shopName, shopPhone, shopProvince, shopId);
        return Math.max(row, 0);
    }
}
